package ly.gupao.edu.java8.example3;

import ly.gupao.edu.java8.Apple;

import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.Predicate;

/**
 * @author: liyang
 * @date：2018/9/4 0004
 * describe：
 */
public class Example_3_Predicate {

    public static void main(String[] args) throws IOException {
        System.out.println(processFile((BufferedReader bufferedReader) -> bufferedReader.readLine()));
        List<Apple> list = Arrays.asList(new Apple("", 1.2), new Apple("green", 2.2));
        Predicate<Apple> nonEmptyColor = (Apple a) -> a.getColor() != "" ;
        List<Apple> newList = filter(list, nonEmptyColor);
        System.out.println(newList);
    }

    public static <T> List<T> filter(List<T> list, Predicate<T> predicate){
        List<T> newList = new ArrayList();
        list.stream().forEach( e -> {
            if(predicate.test(e)){
                newList.add(e);
            }
        });
        return newList;
    }

    public static String processFile(BufferedReaderProcess process) throws IOException {
        try(BufferedReader bufferedReader = new BufferedReader(new FileReader("E:\\EclipseWorkSpace\\Gupao-Learn\\src\\main\\java\\ly\\gupao\\edu\\java8\\example3\\test.txt"))){
            return process.BufferedReaderProcess(bufferedReader);
        }
    }

}
